package com.cgg.designpatterns.iterator;

import java.util.Iterator;

/**
 * @author Xiecheng create on 2021/6/27
 *
 * 容器. 以书架为例
 */
public class BookShelf implements Aggregate<Book>{

    private final Book[] books;
    private int last = 0;

    public BookShelf(int size) {
        this.books = new Book[size];
    }

    @Override
    public Iterator<Book> iterator() {
        return new BookShelfIterator(this);
    }

    public int getSize() {
        return this.last;
    }

    public void append(Book book) {
        this.books[last] = book;
        last ++;
    }

    public Book getBook(int index) {
        return books[index];
    }
}
